Imports System.Collections.Generic
Imports Cdm.DataCore.Entities

Partial Class Case_caseFMSA
    Inherits System.Web.UI.UserControl
    Implements IPermission

    Public ReadOnly Property SectionName() As String Implements IPermission.SectionName
        Get
            Return EntityController.GetSingleDisplayName("FMSA")
        End Get
    End Property

    Public ReadOnly Property ControlCollection() As System.Web.UI.ControlCollection Implements IPermission.ControlCollection
        Get
            Return Me.Controls
        End Get
    End Property

    Public ReadOnly Property EntityPermissionType() As Cdm.DataCore.UserSecurity.Permission.PermissionType Implements IPermission.EntityPermissionType
        Get
            Return Cdm.DataCore.UserSecurity.Permission.PermissionType.CaseFMSA
        End Get
    End Property

    Public Property PrimaryObject() As Cdm.DataCore.ConfigurableObject Implements IPermission.PrimaryObject
        Get
            Dim pg As IPermission
            pg = Me.Page
            Return pg.PrimaryObject
        End Get
        Set(ByVal value As Cdm.DataCore.ConfigurableObject)
            Dim pt As Cdm.DataCore.Entities.Participant
            pt = value
            Me.FMSAId = pt.FMSAId
        End Set
    End Property

    Public ReadOnly Property FMSAList() As List(Of FMSA)
        Get
            Return FMSA.ListAll(0, "Select FMSA")
        End Get
    End Property

    Public Property FMSAId() As Integer
        Get
            Try
                Return CLng(ViewState("fmsaid"))
            Catch ex As Exception
                Return 0
            End Try
        End Get
        Set(ByVal value As Integer)
            BindDropDown()
            ViewState("fmsaid") = value
            'If Me.FmsaDropDownList.Editable Then
            Me.FmsaDropDownList.Value = value.ToString()
            'End If
            SetPageProperties()

        End Set
    End Property

    Protected Sub Page_Init(ByVal sender As Object, _
                                ByVal e As System.EventArgs) _
                                Handles Me.Init

        If Me.IsPostBack Then
            SetEditable()
        Else
            'Replace the FMSA Name with the proper setting from the admin.
            Dim fmsaTitle As String = EntityController.GetSingleDisplayName("FMSA")
            Me.FmsaDropDownList.Title = fmsaTitle
            Me.FMSANameLabel.Text = fmsaTitle
        End If

    End Sub

    Private Sub BindDropDown()

        If FmsaDropDownList.Value = "" Then
            Me.FmsaDropDownList.DataSource = Me.FMSAList
            Me.FmsaDropDownList.DataTextField = "AgencyName"
            Me.FmsaDropDownList.DataValueField = "FMSAId"
            Me.FmsaDropDownList.DataBind()
        End If

    End Sub

    Public Sub SetPageProperties()

        SetEditable()
        SetPanel()

    End Sub

    Private Sub SetPanel()

        If Me.FMSAId = 0 Then

            Me.FmsaDetailsPanel.Visible = False

        Else

            Dim ParticipantFMSA As Cdm.DataCore.Entities.FMSA
            ParticipantFMSA = GetSelectedFMSA()
            If Not ParticipantFMSA Is Nothing Then
                Dim countyDesc As String
                If ParticipantFMSA.CountyId = 0 Then
                    countyDesc = ""
                Else
                    countyDesc = Cdm.DataCore.LookupTables.LuCounty.Retrieve(ParticipantFMSA.CountyId).Description.ToString()
                End If
                Me.FmsaDetailsPanel.Visible = True
                Me.Address1ValueLabel.Text = ParticipantFMSA.Address1
                Me.Address2ValueLabel.Text = ParticipantFMSA.Address2
                Me.CityValueLabel.Text = ParticipantFMSA.City
                Me.CountyValueLabel.Text = countyDesc
                Me.FirstNameValueLabel.Text = ParticipantFMSA.ContactFirstName
                Me.LastNameValueLabel.Text = ParticipantFMSA.ContactLastName
                'Me.MiddleNameValueLabel = ParticipantFMSA.
                Me.StateValueLabel.Text = ParticipantFMSA.State
                Me.ZipCodeValueLabel.Text = ParticipantFMSA.Zip
                Me.PhoneValueLabel.Text = FormatPhoneNumber(ParticipantFMSA.PhoneNumber)
                Me.FaxValueLabel.Text = FormatPhoneNumber(ParticipantFMSA.FaxNumber)
                Me.EmailValueLabel.Text = ParticipantFMSA.EmailAddress
            End If

        End If

    End Sub


    Protected Function FormatPhoneNumber(ByVal phoneNumberString As String) As String
        Dim phoneNumber As String = ""
        Try
            phoneNumber = "(" + phoneNumberString.Substring(0, 3) + ")" + phoneNumberString.Substring(3, 3) + "-" + phoneNumberString.Substring(6)
        Catch ex As Exception
        End Try
        Return phoneNumber
    End Function

    Private Function GetSelectedFMSA() As Cdm.DataCore.Entities.FMSA

        Dim i As Integer
        For i = 0 To Me.FMSAList.Count - 1
            If Me.FMSAList(i).FMSAId = Me.FMSAId Then
                Return FMSAList(i)
            End If
        Next

        Return Nothing

    End Function

    Private Sub SetEditable()

        Me.FmsaDropDownList.Editable = UserAuthorization.HasUpdatePermission(Cdm.DataCore.UserSecurity.Permission.PermissionType.CaseFMSA)
        If FmsaDropDownList.Editable = False Then
            Me.SubmitFMSAButton.Visible = False
        End If

    End Sub

    Public Sub ResetControls() Implements IPermission.ResetControls
        'used to reset the controls to preserve viewstate
        'don't believe this needs any code on this page.
    End Sub

    Protected Sub FMSADropdown_SelectedIndexChanged() Handles FmsaDropDownList.SelectedIndexChanged

        ViewState("fmsaid") = FmsaDropDownList.Value
        SetPanel()

    End Sub

End Class
